package org.ssp.util;

import org.ssp.pojo.BaseButton;
import org.ssp.pojo.ClickButton;
import org.ssp.pojo.ComplexButton;
import org.ssp.pojo.Menu;
import org.ssp.pojo.ViewButton;

import net.sf.json.JSONObject;

/**
 * 菜单工具类
 * @author susp
 *
 */
public class MenuUtil {
	private final static String MENU_CREATE_URL="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	private final static String MENU_QUERY_URL="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
	private final static String MENU_DELETE_URL="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

	/**
	 * 
	 * 创建菜单
	 * @param json 菜单结构
	 * @param token access_token
	 * @return  若菜单创建成功，返回true
	 * @throws Exception 
	 */
	public static boolean createMenu(String json,String access_token) throws Exception{
		Boolean result=false;
	    String url=MenuUtil.MENU_CREATE_URL.replace("ACCESS_TOKEN", access_token);
	    System.out.println(url);
		String str=CommonUtil.httpsRequest(url, "POST", json);		
//		通过json-lib的API把json字符串转化成java对象
		JSONObject jsonObject=JSONObject.fromObject(str);
		if(null!=jsonObject){
			int errcode=jsonObject.getInt("errcode");
			String str1=jsonObject.getString("errmsg");
			
			if(errcode==0){
				result=true;
			}else{
				System.out.println("创建菜单失败"+errcode+"\t"+str1);
			}
			
		}
		
		return result;
		
	}
	/**
	 * 菜单对象转化成json格式数据
	 * @return
	 */
	public static String  objectToJson(){
		
		ClickButton click1=new ClickButton();
		click1.setName("天气预报");
		click1.setType("click");
		click1.setKey("KEY_WEATHER");
		
		ClickButton click2=new ClickButton();
		click2.setName("每日歌曲");
		click2.setType("click");
		click2.setKey("KEY_MUSIC");
		
		ViewButton view=new ViewButton();
		view.setName("百度");
		view.setType("view");
		view.setUrl("http://www.baidu.com/");
		
		BaseButton[] com_btn={click2,view};

		ComplexButton complex=new ComplexButton();
		complex.setName("菜单");
		complex.setSub_button(com_btn);
		
		BaseButton[] menu_btn={click1,complex};
		Menu menu=new Menu();
		menu.setButton(menu_btn);
		
		//将java对象转成json字符串，json-lib api
		String json=JSONObject.fromObject(menu).toString();
		return json;
		
		
	}
	
	/**
	 * 查询菜单
	 * @param access_token
	 * @return
	 * @throws Exception
	 */
	public static String queryMenu(String access_token) throws Exception{
	    String url=MenuUtil.MENU_QUERY_URL.replace("ACCESS_TOKEN", access_token);
	    System.out.println(url);
		String respJson=CommonUtil.httpsRequest(url, "GET",null);
		return respJson;
		
	}
	/**
	 * 删除菜单
	 * @param access_token
	 * @return
	 * @throws Exception
	 */
	
	public static boolean deleteMenu(String access_token) throws Exception{
		boolean result=false;
		String url=MenuUtil.MENU_DELETE_URL.replace("ACCESS_TOKEN", access_token);
		String respJson=CommonUtil.httpsRequest(url, "GET", null);
		JSONObject jsob=JSONObject.fromObject(respJson);
		if(null!=jsob){
			int errorcode=jsob.getInt("errcode");
			String errmsg=jsob.getString("errmsg");
			if(errorcode==0){
				result=true;
				
			}else{
				System.out.println("菜单闪删除失败"+"\t"+errorcode+"\t"+errmsg);
				
			}
			
		}
		
		return result;
	}
	
	
	

}
